package com.fist.eth.flutter_plugin_eth_wallet.utils

import com.google.gson.Gson
import org.json.JSONException
import org.json.JSONObject
import org.web3j.crypto.Bip39Wallet
import org.web3j.crypto.ECKeyPair
import org.web3j.crypto.Keys
import org.web3j.crypto.WalletFile

class TokenWallet : Bip39Wallet {
    private lateinit var mWalletFile: WalletFile
    private lateinit var mKeyPair: ECKeyPair

    constructor(walletFile: WalletFile, mnemonic: String) : super(null, mnemonic) {
        this.mWalletFile = walletFile
    }

    constructor(walletFile: WalletFile, mnemonic: String, keyPair: ECKeyPair) : super(null, mnemonic) {
        this.mWalletFile = walletFile
        this.mKeyPair = keyPair
    }

    constructor(filename: String, mnemonic: String) : super(filename, mnemonic)

    val keyPair: ECKeyPair get() = mKeyPair

    val keyStoreJsonString: String
        get() {
            val gson = Gson()
            var j: JSONObject? = null
            try {
                j = JSONObject(gson.toJson(mWalletFile))
                j.remove("address")
            } catch (e: JSONException) {
                e.printStackTrace()
            }

            return j!!.toString()
        }
    val address: String get() = Keys.toChecksumAddress(mWalletFile.address)

    val walletFile: WalletFile get() = mWalletFile
}
